package com.example.nuc_ms_beta.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.nuc_ms_beta.R;

import com.example.nuc_ms_beta.ui.viewconstruction.mainfragment.MyFragment;
import com.example.nuc_ms_beta.ui.viewconstruction.mainfragment.NewsFragment;
import com.example.nuc_ms_beta.ui.viewconstruction.mainfragment.VideoFragment;
import com.example.nuc_ms_beta.ui.viewcomponent.slidmenuview.LeftScrolSlideMenu;

/**
 * 主界面
 */
public class MainActivity extends FragmentActivity implements android.view.View.OnClickListener {
    private LeftScrolSlideMenu leftScrolSlideMenu;
    private Fragment tab_one;
    private Fragment tab_two;
    private Fragment tab_three;
    private LinearLayout bottom_layout_one;
    private LinearLayout bottom_layout_two;
    private LinearLayout bottom_layout_three;
    private ImageView bottom_iv_one;
    private ImageView bottom_iv_two;
    private ImageView bottom_iv_three;
    private TextView bottom_tv_one;
    private TextView bottom_tv_two;
    private TextView bottom_tv_three;
    private static boolean isExit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initOnclick();
        SetFragment(1);
    }

    private void initOnclick() {
        bottom_layout_one.setOnClickListener(this);
        bottom_layout_two.setOnClickListener(this);
        bottom_layout_three.setOnClickListener(this);
    }

    //This a test

    //初始化
    private void init() {
        leftScrolSlideMenu = (LeftScrolSlideMenu) findViewById(R.id.leftMenu);
        bottom_layout_one = (LinearLayout) findViewById(R.id.bottom_layout_one);
        bottom_iv_one = (ImageView) findViewById(R.id.bottom_iv_one);
        bottom_tv_one = (TextView) findViewById(R.id.bottom_tv_one);

        bottom_layout_two =  (LinearLayout) findViewById(R.id.bottom_layout_two);
        bottom_iv_two = (ImageView) findViewById(R.id.bottom_iv_two);
        bottom_tv_two = (TextView) findViewById(R.id.bottom_tv_two);

        bottom_layout_three = (LinearLayout) findViewById(R.id.bottom_layout_three);
        bottom_iv_three = (ImageView) findViewById(R.id.bottom_iv_three);
        bottom_tv_three = (TextView) findViewById(R.id.bottom_tv_three);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_layout_one:
                SetFragment(1);
                break;
            case R.id.bottom_layout_two:
                SetFragment(2);
                break;
            case R.id.bottom_layout_three:
                SetFragment(3);
                break;
        }
    }

    private void SetFragment(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        HideFragment(transaction);
        switch (i) {
            case 1:
                if (tab_one == null) {
                    tab_one = new NewsFragment(this, leftScrolSlideMenu);
                    transaction.add(R.id.main_fragment, tab_one);
                } else {
                    transaction.show(tab_one);
                }
                break;
            case 2:
                if (tab_two == null) {
                    tab_two = new VideoFragment(this);
                    transaction.add(R.id.main_fragment, tab_two);
                } else {
                    transaction.show(tab_two);
                }
                break;
            case 3:
                if (tab_three == null) {
                    tab_three = new MyFragment(this);
                    transaction.add(R.id.main_fragment, tab_three);
                } else {
                    transaction.show(tab_three);
                }
                break;
        }
        transaction.commit();
    }

    private void HideFragment(FragmentTransaction transaction) {
        if (tab_one != null) {
            transaction.hide(tab_one);
        }
        if (tab_two != null) {
            transaction.hide(tab_two);
        }
        if (tab_three != null) {
            transaction.hide(tab_three);
        }
    }
}

